<template>
<div class="wrap">
  <div class="container">
    <img :src="shareImg" class="shareImg"/>
    <ul>
      <li @click="goDiscuss">
          <img src="http://file.rzkeji.com/web/daka/icon/icon-back.png"/>
          <span>跳转页面</span>
      </li>
      <li @click="save">
          <img src="http://file.rzkeji.com/web/daka/icon/share-download.png"/>
          <span>保存相册</span>
      </li>
       <li>
        <button open-type="share">
          <img src="http://file.rzkeji.com/web/daka/icon/wxhaoyou.png"/>
          <span>微信好友</span> 
        </button>
      </li>
    </ul>
  </div>
</div>
</template>
<script>
export default {
  data () {
    return {
    shareImg:''
  }
 },
  methods: {
    goDiscuss(){
      wx.navigateTo({
        url:"../logs/main"
      })
    },
    save(){
      var _this=this;
      wx.getSetting({     //查看是否开启保存相册的权限
        success(res){
          if(!res.authSetting['scope.writePhotosAlbum'])   //如果没有开启，则开启
          {
            wx.authorize({        //开启权限
              scope:'scope.writePhotosAlbum',
              success(res){
              console.log('授权成功')
              }
            })
          }
        }
      });
      wx.downloadFile({     //调用下载文件的接口
        url:_this.shareImg,    //这里的img地址的网络地址
        success(res){ 
          wx.saveImageToPhotosAlbum({      //保存图片到本地
            filePath:res.tempFilePath,
            success(res){
                wx.showToast({title:"下载成功"});
            }
          })
        }
     })
    }
  },
  onShow: function(option){
    var _this = this;
    //这里是生成分享图
    wx.showLoading({
      title:"加载图片中",
      mask:true
    })
    var userToken=wx.getStorageSync('userToken');
    var dakaId=_this.$store.state.dakaId;
    wx.request({
      url:'https://daka.rzkeji.com/api/daka/buildShareImg',
      data:{
          page:'pages/index/main',
          id:dakaId,
          daka_token:userToken
      },
      method:'post',
      header: {
          'content-type': 'application/json' // 默认值
      },
      success(res){
          _this.shareImg=res.data.data;
          wx.hideLoading()
      }
    })
  },
  onShareAppMessage(){
    wx.showShareMenu({
      withShareTicket:true
    })
    return {
      title:'分享打卡详细',
      path:'/pages/index/main',
      success(res){
        wx.showToast({
          title:"转发成功",
          icon:'success',
          duration:1500
        })
      }
    }
  },
}
</script>
<style scoped>
.wrap{width: 100%;height: 100%;}
.container{width: 80%;height: 375px;position: absolute;top: 30px;left: 50%;margin-left: -40%;}
.shareImg{width: 100%;height: 400px;}
ul{width: 90%;margin-top: 40px;height: 80px;display: flex;margin-left: 5%;justify-content: space-around}
ul li{width: 70px;height: 80px;}
button{width: 100%;height: 100%;background: white;display: inline;padding-left:0;line-height: 20px;padding-right: 0px;}
ul li img{width: 60px;height: 60px;margin-left: 5px}
ul li span{width: 70px;height: 20px;font-size: 14px;display: block;text-align: center}
</style>
